package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.SeckillProducts;
import com.example.mall.dto.SeckillProductsDto;
import com.example.mall.mapper.SeckillProductsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SeckillProductsQuery;
import com.example.mall.service.ISeckillProductsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 * 秒杀商品表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class SeckillProductsServiceImpl extends ServiceImpl<SeckillProductsMapper, SeckillProducts> implements ISeckillProductsService {

    @Autowired
    private SeckillProductsMapper seckillProductsMapper;

    @Override
    public PageDTO<SeckillProductsDto> queryPage(SeckillProductsQuery seckillProductsQuery) {
        if (seckillProductsQuery == null) {
            seckillProductsQuery = new SeckillProductsQuery();
        }
        Page<SeckillProducts> page = seckillProductsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SeckillProducts> cartPage = lambdaQuery()
                .like(seckillProductsQuery.getProductId()!= null, SeckillProducts::getProductId, seckillProductsQuery.getProductId())
                .like(seckillProductsQuery.getSeckillId()!= null, SeckillProducts::getSeckillId, seckillProductsQuery.getSeckillId())
                .like(seckillProductsQuery.getSeckillPrice()!= null, SeckillProducts::getSeckillPrice, seckillProductsQuery.getSeckillPrice())
                .like(seckillProductsQuery.getSeckillStock()!= null, SeckillProducts::getSeckillStock, seckillProductsQuery.getSeckillStock())
                .like(seckillProductsQuery.getActivityId()!= null, SeckillProducts::getActivityId, seckillProductsQuery.getActivityId())
                .like(seckillProductsQuery.getOriginalPrice()!= null, SeckillProducts::getOriginalPrice, seckillProductsQuery.getOriginalPrice())
                .page(page);
        return PageDTO.of(cartPage, SeckillProductsDto.class);
    }

    @Override
    public SeckillProducts querySeckillProductsById(Long seckillId) {
        AssertUtil.isNotNull(seckillId, "seckillId不能为空");
        return seckillProductsMapper.selectById(seckillId);
    }

    @Override
    public List<SeckillProducts> querySeckillProductsList() {
        return this.list();
    }

    @Override
    public void insert(SeckillProductsDto seckillProductsDto) {
        SeckillProducts seckillProducts = new SeckillProducts();
        CopyUtils.copyProperties(seckillProductsDto, seckillProducts);
        seckillProducts.setSeckillId(CodeGenerateUtils.generateUniqueId());
        seckillProducts.setCreateTime(new Date());
        seckillProducts.setUpdateTime(new Date());
        seckillProductsMapper.insert(seckillProducts);
    }

    @Override
    public void update(SeckillProductsDto seckillProductsDto) {
        AssertUtil.isNotNull(seckillProductsDto.getSeckillId(), "seckillId不能为空");
        SeckillProducts seckillProducts = seckillProductsMapper.selectById(seckillProductsDto.getSeckillId());
        AssertUtil.isNotNull(seckillProducts, "秒杀商品不存在");
        CopyUtils.copyProperties(seckillProductsDto, seckillProducts);
        seckillProducts.setUpdateTime(new Date());
        seckillProductsMapper.updateById(seckillProducts);
    }

    @Override
    public void delete(Long seckillId) {
        AssertUtil.isNotNull(seckillId, "seckillId不能为空");
        SeckillProducts seckillProducts = seckillProductsMapper.selectById(seckillId);
        AssertUtil.isNotNull(seckillProducts, "秒杀商品不存在");
        seckillProductsMapper.deleteById(seckillId);
    }

    @Override
    public void deleteBatch(List<Long> seckillIds) {
        AssertUtil.isNotNull(seckillIds, "seckillIds不能为空");
        List<SeckillProducts> seckillProducts = seckillProductsMapper.selectBatchIds(seckillIds);
        AssertUtil.isNotNull(seckillProducts, "秒杀商品不存在");
        seckillProductsMapper.deleteBatchIds(seckillIds);
    }
}
